package com.leapp.sk.viewdelegate.classs

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import cc.vv.lkbasecomponent.util.LKScreenUtil
import cc.vv.lklibrary.log.LogOperate
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.ViewTarget
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.SKTopBar
import com.example.mvp.view.imgselector.album.widget.HackyViewPager
import com.example.mvp.view.imgselector.model.entity.ImageInfo
import com.example.mvp.view.photoview.PhotoView
import com.example.util.ISTECHER
import com.leapp.sk.R
import com.leapp.sk.ui.activity.classs.ClassAlbumPreviewActivity
import com.leapp.sk.util.ShareDialogUtils
import java.util.ArrayList

/**
 * 班级相册图片预览视图托管
 * @author  chaohaipeng  on 2018/8/7.
 */
class ClassAlbumPreviewDelegate : BaseAppDelegate(){
    /**
     * 标题栏
     */
    var topBar: SKTopBar? = null
    /**
     * 数据源
     */
    var mData: ArrayList<ImageInfo>? = null

    /**
     * 当前位置
     */
    var mCurrentPosition: Int = 0

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_class_album_preview
    }

    /**
     * 初始化适配器
     */
    fun initAdapter(context: Context, mCurrentPosition: Int, data: ArrayList<ImageInfo>) {
        mData = data
        val hvp_acap_pager = getView<HackyViewPager>(R.id.hvp_acap_pager)
        val mPagerAdapter = ImageDetailAdapter(Glide.with(context), data)
        hvp_acap_pager.adapter = mPagerAdapter
        hvp_acap_pager.currentItem = mCurrentPosition
        hvp_acap_pager.addOnPageChangeListener(onPageChangeListener)
        tobTitle(" ${mCurrentPosition + 1}/" + data.size)
    }

    /**
     * 设置顶部标题
     */
    private fun tobTitle(title: String) {
        val topBar = getView<SKTopBar>(R.id.jtb_acap)
        topBar.setTitle(title)
    }

    /**
     * 设置标题
     */
    fun initTobBar(context: Activity, listener: ShareDialogUtils.SharedListener) {
        if (ISTECHER){
            ShareDialogUtils.getInstance().initSharedDialog(context, "www.baidu.com", listener,true)
        }else{
            ShareDialogUtils.getInstance().initSharedDialog(context, "www.baidu.com", listener,false)
        }

        topBar = getView<SKTopBar>(R.id.jtb_acap)
        topBar?.setRightBtnImage(R.mipmap.icon_album_detial)


        topBar?.setRightBtnImageShow(true)
        topBar?.setEventInterface(object : SKTopBar.EventInterface {
            override fun leftOnClick() {
                getActivity<ClassAlbumPreviewActivity>().finish()
            }

            override fun rightOnClick() {
                ShareDialogUtils.getInstance().showDialog()
            }
        })

    }

    private val onPageChangeListener = object : ViewPager.SimpleOnPageChangeListener() {
        override fun onPageSelected(position: Int) {
            mCurrentPosition = position + 1
            val size = mData?.size
            if (size != null) {
                if (size > 0) {
                    topBar?.setTitle(" ${mCurrentPosition}/" + size)
                } else {
                    topBar?.setTitle("0/" + 0)
                }


            } else {
                topBar?.setTitle("0/" + 0)

            }


        }
    }

    /**
     * 图片详情适配器
     */
     class ImageDetailAdapter: PagerAdapter {

        private var mData = ArrayList<ImageInfo>()

        private val mRequestManager: RequestManager

        /**
         * 图片加载操作
         */
        var mRequestOptions: RequestOptions = RequestOptions()

        constructor(requestManager: RequestManager, data: ArrayList<ImageInfo>) {
            mRequestManager=requestManager
            mData=data
            mRequestOptions.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
            mRequestOptions.skipMemoryCache(true)
            mRequestOptions.override(LKScreenUtil.getScreenWidth(),LKScreenUtil.getScreenHeight())
            mRequestOptions.error(R.mipmap.icon_banner)
            mData = checkNotNull<ArrayList<ImageInfo>>(data)
        }

        override fun getCount(): Int {
            return mData.size
        }

        override fun isViewFromObject(view: View, `object`: Any): Boolean {
            return view === `object`
        }

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val photoView:ImageView = LayoutInflater.from(container.context)
                    .inflate(R.layout.adapter_class_album_preview_item, container, false) as PhotoView
            val imageInfo = mData[position]
            mRequestManager
                    .load(imageInfo.path)
                    .thumbnail(0.2f)
                    .apply(mRequestOptions)
                    .into(photoView)
            container.addView(photoView)


            return photoView
        }



        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            val imageView = `object` as PhotoView ?: return
            Glide.with(container.context).clear(imageView)
            container.removeView(`object` as View)
        }

        fun getItem(position: Int): ImageInfo {
            return mData[position]
        }
    }




}